<!-- 
All rights Reserved, Designed By www.youyacao.com 
@Description:用户程序主页面
@author:成都市一颗优雅草科技有限公司     
@version V5.x 
注意：本前端源码遵循 MulanPSL-2.0开源协议（木兰宽松许可证）本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   企业客服QQ:2853810243  官方客户·技术交流群 929353806交流群 929353806
 -->
<template>
	<view class="content">
		<swiper :indicator-dots="false" :duration="300" style="min-height: 100vh;" @change="change" :current="current">
			<swiper-item>
				<home ref="home"></home>
			</swiper-item>
			<swiper-item>
				<collection ref="collection"></collection>
			</swiper-item>
			<swiper-item>
				<channel ref="channel"></channel>
			</swiper-item>
			<swiper-item>
				<user ref="user"></user>
			</swiper-item>
		</swiper>
		<modal v-model="showModal" :mData="selectModalData" :type="type" @onConfirm="onConfirm" @cancel="onCancel" :nav="true">
		</modal>
		<footerNav @change_nav="change_nav"></footerNav>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import footerNav from "@/components/footer/footer_nav.vue"
	import home from "./home/index.vue"
	import collection from "./user/collection/collection.vue"
	import channel from "./channel/index.vue"
	import user from "./user/index.vue"
	import modal from '@/components/modal/modal.vue'
	
	export default {
		data() {
			return {
				showModal: false,
				selectModalData: [],
				sysheight: 0,
				type: 'select'
			}
		},
		components: {
			footerNav,
			home,
			collection,
			channel,
			user,
			modal
		},
		created() {
			this.sysheight = uni.getSystemInfoSync().windowHeight
		},
		onLoad() {
			uni.$on("showModelData", (data) => {
				this.clickItem = data.clickItem;
				this.selectModalData = data.data;
				this.type = data.type;
				this.showModal = true;
			})
			uni.$on('loadMore', (data) => {
				this.$refs.home.onLoadMore();
			})
			let user = uni.getStorageSync('user');
			if (user) {
				this.$store.commit('setUser', user);
				// IM注册
				// this.IMRegister(user);
			}
		},
		onReachBottom() {
			var $page = this.$store.state.footer_store.now_page_index;
			var $page = this.page_code;
			switch ($page) {
				case "home":
					this.$refs.home.onLoadMore();
					break;
				case "collection":
					this.$refs.collection.onLoadMore();
					break;
				case "user":
					this.$refs.user.onLoadMore();
					break;
			}
		},
		computed: {
			current() {
				var index = this.$store.state.footer_store.now_page_index;
				let length = this.$store.state.footer_store.footer_nav.length;
				if (index == Math.floor(length / 2) || index > Math.floor(length / 2)) {
					//中心按钮点击
					return index - 1;
				}
				return index;
			},
			page_code() {
				return this.$store.state.footer_store.footer_nav[this.$store.state.footer_store.now_page_index].name_code;
			},
			user() {
				return this.$store.state.user;
			}
		},
		methods: {
			...mapMutations(['IMlogin']),
			// ImLogin
			IM_login(userInfo) {
				var loginparams = {
					"username": userInfo.username,
					"password": userInfo.password
				}
				uni.showLoading();
				this.jpushIM.userLogin(loginparams, (res) => {
					uni.hideLoading();
					if (res.errorCode == 0) {
						this.jpushIM.updateMyInfo({
							'extras': {
								"head_img": userInfo.avatar
							}
						}, (res) => {
							console.log("updataHeadImg");
							console.log(res);
						})
						this.IMlogin(userInfo.username);
						console.log("IM登录成功")
					} else {
						uni.showModal({
							title: 'IM登录失败',
							content: "原因：" + res.errorMsg,
							showCancel: false,
							cancelText: '',
							confirmText: '确定',
							success: res => {
								if (res.confirm) {
									console.log("点击了确定按钮");
								}
							}
						});
					}
				})
			},
			// IMRegister
			IMRegister(userInfo) {
				var IMUserInfo;
				if (userInfo.phone) {
					IMUserInfo = userInfo.phone;
				} else if (userInfo.mail) {
					IMUserInfo = userInfo.mail;
				} else if (userInfo.qq) {
					IMUserInfo = userInfo.qq;
				} else {
					IMUserInfo = userInfo.name;
				}
				var params = {
					"username": IMUserInfo,
					"password": IMUserInfo,
					"nickname": userInfo.name,
					"avatar": userInfo.head_img,
					"extras": {
						"head_img": userInfo.head_img
					}
				}
				uni.showLoading();
				this.jpushIM.userRegister(params, (res) => {
					uni.hideLoading();
					console.log("------------");
					if (res.errorCode == 0 || res.errorCode == 898001) {
						this.IM_login(params);
					} else {
						uni.showModal({
							title: 'IM注册失败',
							content: "原因：" + res.errorMsg,
							showCancel: false,
							cancelText: '',
							confirmText: '确定',
							success: res => {
								if (res.confirm) {
									console.log("点击了确定按钮");
								}
							}
						});
					}
				})
			},
			onConfirm(e) {
				this.$refs.home.onConfirm(e);
			},
			onCancel() {},
			change(e) {
				var index = e.detail.current;
				let length = this.$store.state.footer_store.footer_nav.length;
				if (index == Math.floor(length / 2) || index > Math.floor(length / 2)) {
					//中心按钮点击
					this.$store.commit("change_page", index + 1)
					return;
				}
				//console.log(index);
				this.$store.commit("change_page", index)
			},
			change_nav(index) {
				let length = this.$store.state.footer_store.footer_nav.length;
				if (index == Math.floor(length / 2)) {
					//中心按钮点击

				 this.centerClick();
					return;
				}
				console.log("索引",index)
				this.$store.commit("change_page", index)
			},
			centerClick() {
				if (this.user == null) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}
				uni.navigateTo({
					url: '/pages/index/user/category-search/category-search'
				})
				// uni.navigateTo({
				// 	url: 'publish/publish'
				// })
			}
		},
		onPullDownRefresh() {
			//console.log('index refresh');
			var $page = this.page_code;
			switch ($page) {
				case "home":
					this.$refs.home.refresh();
					break;
				case "collection":
					this.$refs.collection.refresh();
				case "user":
					this.$refs.user.refresh();
					break;
			}
		}
	}
</script>

<style></style>
